<html>
<head>
	<title>BlackJackX</title>
	<link rel="stylesheet" type="text/css" href="styles.css">
	<?php 
		include('connect.php');
		
		session_start();
		
		$sessionID = $_SESSION['SessionID'];		
		
		// Usernamen aus Datenbank lesen
		$anfrage = "SELECT Username from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$userName = mysql_result($ergebnis, 0); /** mysql_result ist set PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
		
		// Derzeitiges Guthaben des Spielers aus der Datenbank lesen
		$anfrage = "SELECT Cash from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$cash = mysql_result($ergebnis, 0); /** mysql_result ist seit PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
		
		// Derzeitiges Guthaben des Spielers aus der Datenbank lesen
		$anfrage = "SELECT Level from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$level = mysql_result($ergebnis, 0); /** mysql_result ist seit PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
		
		// Derzeitige Anzahl an Freunden des Spielers aus Datenbank lesen
		$anfrage = "SELECT * FROM tblfriends WHERE ID LIKE '$sessionID'";
		$ergebnis = mysql_query($anfrage);
		$friendstotal = mysql_num_rows($ergebnis);
		
		// Derzeitige Anzahl an Freunden des Spielers, die online sind aus Datenbank lesen
		$idList = array();		
		$onlineList = array();
		$sql = mysql_query("SELECT * FROM tblfriends WHERE ID LIKE '$sessionID'");
		
		while($row = mysql_fetch_array($sql,MYSQL_ASSOC)) {
			$friendID = $row['FriendID'];
			array_push($idList, $friendID);
		}
		
		for($i = 0; $i < count($idList); $i++) {
			$sql = mysql_query("SELECT Username FROM tblusers WHERE ID LIKE '$idList[$i]' AND Online LIKE '1'");
			if(mysql_num_rows($sql) > 0) {
				array_push($onlineList, mysql_result($sql, 0));
			}
		}
		
		$friendsonline = count($onlineList);
		
	?>
	<script type="text/javascript">
		function addFriend() {
			var friend = prompt("Please enter your friend's username", "Username");			
			
			if(friend != null) {
				if(friend === "<?php echo $userName ?>") {
				alert('You cannot add yourself as a friend. Forever alone! :(');
				} else {
					addFriendToDB(friend);					
				}
			}
		}
		function addFriendToDB(friend) {
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
			}
			else { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
			}
			xmlhttp.onreadystatechange = function() {
				
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
					if(xmlhttp.responseText == "success") {
						var friendstotal = parseInt(document.getElementById("friendstotal").innerHTML, 10);
						document.getElementById("friendstotal").innerHTML = friendstotal + 1;
						document.getElementById("msg").innerHTML = "You now have a new friend!";
						/*Freundesliste aktualisieren*/
						updateFriendslist();						
					}
					else if (xmlhttp.responseText == "error_already_friends"){						
						document.getElementById("msg").innerHTML = "You already know that person!";
					}
					else if (xmlhttp.responseText == "error_user_not_found") {
						document.getElementById("msg").innerHTML = "Person does not exist!";
					}
				}
			}			
			
			xmlhttp.open("GET","addFriendToDB.php?userName="+"<?php echo $userName?>"+"&friend="+friend,true);			
			xmlhttp.send();			
		}
		
		function deleteFriend() {
			var friend = prompt("Please enter your friend's username", "Username");			
			
			if(friend != null) {
				if(friend === "<?php echo $userName ?>") {
				alert('You cannot delete yourself as a friend, emo! :(');
				} else {
					deleteFriendFromDB(friend);
					
				}
			}
		}
		
		function deleteFriendFromDB(friend) {
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
			}
			else { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
			}
			xmlhttp.onreadystatechange = function() {
				
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {	
					if(xmlhttp.responseText == "success") {
						var friendstotal = parseInt(document.getElementById("friendstotal").innerHTML, 10);
						document.getElementById("friendstotal").innerHTML = friendstotal - 1;
						document.getElementById("msg").innerHTML = "You have lost a friend! :(";						
						/*Freundesliste aktualisieren*/
						updateFriendslist();
					}
					else if (xmlhttp.responseText == "error_user_not_found") {
						document.getElementById("msg").innerHTML = "Person does not exist!";
					}
				}
			}			
			
			xmlhttp.open("GET","deleteFriendFromDB.php?userName="+"<?php echo $userName?>"+"&friend="+friend,true);			
			xmlhttp.send();			
		}
		
		function logout() {
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
			}
			else { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
			}	
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					window.location.replace("index.php");
				}
			}
			
			xmlhttp.open("GET","logout.php",true);			
			xmlhttp.send();
		}
		
		function updateFriendslist() {
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
			}
			else { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
			}	
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					document.getElementById("friendslist").innerHTML = xmlhttp.responseText;
					/* Anzahl der Freunde, die online sind aktualisieren */
					updateFriendsOnline();
				}
			}
			
			xmlhttp.open("GET","updateFriends.php",true);			
			xmlhttp.send();
		}
		
		function updateFriendsOnline() {
			if (window.XMLHttpRequest) {
					xmlhttp = new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
				}
				else { 
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
				}	
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						document.getElementById("friendsonline").innerHTML = xmlhttp.responseText;
					}
				}
				
				xmlhttp.open("GET","updateFriendCount.php",true);			
				xmlhttp.send();
		}
	</script>
	</head>
	<body onload="updateFriendslist();">
			<div name="logout" class="logout">
			<input name="logout" id="logout" class="button" type="submit" value ="Logout" onclick="logout();">
		</div>
	<div name="frame" class="frame">
	
		<div name="userCash" class="cash">
			<?php echo $cash ?> $
		</div>

		<div name="userData" class="userData">
		<table>
				<tr>
					<td>
						Name:
					</td>
					<td>
						<?php echo $userName ?>
					</td>
				</tr>
				<tr>
					<td>
						Cash:
					</td>
					<td>
						<?php echo $cash ?>
					</td>
				</tr>
				<tr>
					<td>
						Level:
					</td>
					<td>
						<?php echo $level ?>
					</td>
				</tr>			
				<tr>
					<td>
						Friends total:
					</td>
					<td>
						<span id="friendstotal"><?php echo $friendstotal ?></span>
					</td>
				</tr>
				<tr>
					<td>
						Friends online:
					</td>
					<td>
						<span id="friendsonline"><?php echo $friendsonline?></span>
					</td>
				</tr>			
				<tr>
					<td>
						<input name="addfriend" id="addfriend" class="button" type="submit" value="Add a Friend!" onclick="addFriend();">
					</td>
					<td>
						<input name="deleteFriend" id="deleteFriend" class="button" type="submit" value="Delete a Friend!" onclick="deleteFriend();">
					</td>
				</tr>			
			</table>
			<span id="msg"></span>
		</div>
		<div name="friendslist" id="friendslist" class="friendsList" onMouseOver="updateFriendslist();">		
		</div>	
	</div>
	<div name="tableOne" class="bjtable1" style="left:15%" onclick="location.href='/bj.php';">
		 
	</div>
	<div name="tableTwo" class="bjtable2" style="left:40%" onclick="location.href='/bj.php';">
		
	</div>

	<div name="tableThree" class="bjtable3" style="right:15%" onclick="location.href='/bj.php';">
		
	</div>	
</body>
</html>